package com.sw.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sw.model.AddressManage;
import com.sw.model.User;
import com.sw.model.UserInfo;
import com.sw.service.UserInfoService;

import net.sf.json.JSONObject;


@Controller
@RequestMapping(value="/userInfo")
public class UserInfoController {

	@Autowired
	private UserInfoService userInfoService;
	/**
	 * 查询用户信息
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/getUserInfo/{id}",method = RequestMethod.GET,produces="application/json;charset=UTF-8")
	public @ResponseBody Object getUserInfo(@PathVariable("id") Integer id){
		System.out.println(id);
		User user = userInfoService.getUserInfo(id);
		if(user != null){
			UserInfo userinfo = user.getUserinfo();
			List<AddressManage> addressManages = userinfo.getAddressManages();
			for (AddressManage addressManage : addressManages) {
				if(addressManage.getState() == 1){
					addressManage.getAddressInfo();
				}
			}
		}
		
		JSONObject jsonObject = JSONObject.fromObject(user);
		return jsonObject.toString();
	}
}
